<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
global $data, $admin;

/**
 * Description of empresas
 *
 * @author Golem
 */
class Modulos extends CI_Controller {

    public function __construct() {
        parent::__construct();
        global $admin, $data;
        # cargamos variables
        $admin = $this->session->userdata('admin');
        $admin['place'] = 'modulos';
        # verifica si pasó por sushitto
        if ($admin['ta_tipo_administrador_id'] != 1) {
            redirect('home');
        }
        $this->output->enable_profiler(FALSE);
    }

    public function index() {
        global $data, $admin;

        $data['modulos'] = $this->cc_model_a->lista_modulos();
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('modulos/content');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function alta() {
        global $data, $admin;
        $data['empresa'] = $this->cc_model_a->cEmpresas();
        $data['division'] = $this->cc_model_a->lista_divisiones();
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('modulos/alta');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function save() {
        $this->form_validation->set_rules('m_nombre', 'm_nombre', 'required|trim|xss_clean|max_length[30]');
        $this->form_validation->set_rules('d_division_id', 'd_division_id', 'required|trim|xss_clean|max_length[30]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            redirect('modulos/alta');
        }
        $m_nombre = $this->security->xss_clean($this->input->post('m_nombre'));
        $d_division_id = $this->security->xss_clean($this->input->post('d_division_id'));
        $this->cc_model_a->gModulo($d_division_id, $m_nombre);
        redirect('modulos');
    }

    public function jsonlist() {
        $d_division_id = $this->input->post('name');
        $divisiones = $this->cc_model_a->listaModulos_xDivision($d_division_id);
        $this->output->set_output(json_encode($divisiones));
    }

}
